﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;

public partial class Users : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    int gender = 0;
    int status = 1;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //kiem tra folder co ton tai hay khong
        DirectoryInfo dInfo = new DirectoryInfo(Server.MapPath("../images/"));

        if (!dInfo.Exists)
        {
            dInfo.Create();
        }
        if (!upload.HasFile)
        {
            lblMessage.Text = "Bạn chưa chọn file upload";
            return;
        }
        else
        {
            string ext = System.IO.Path.GetExtension(Server.MapPath(upload.FileName));


            if ((ext == ".JPG") || (ext == ".PNG") || (ext == ".JPEG") || (ext == ".BMP"))
            {
                upload.PostedFile.SaveAs(Server.MapPath("../images/" + upload.FileName));
                //---------------
                Session["img"] = upload.FileName;
                lblMessage.Text = "Upload file thành công";
                Image1.ImageUrl = "images/" + upload.FileName;
            }
            else
            {
                lblMessage.Text = "File upload không phải là phải ảnh";
                return;
            }

        }

    }
    protected void btnThem_Click(object sender, EventArgs e)
    {

        try
        {
            if (rdoNam.Checked)
            {
                gender = 1;
            }
            if (rdoLock.Checked) {
                status = 0;
            }

              var check = db.Users.Where(c => c.UserID == txtUsername.Text).SingleOrDefault();
              if (check != null)
              {
                  lblFalse.Text = "Ten dang nhap da ton tai...";
                  Panel2.Visible = true;
                  Panel1.Visible = false;
              }
              else
              {
                  User u = new User();
                  u.UserID = txtUsername.Text;
                  u.Password = txtPassword.Text;
                  u.FullName = txtFullName.Text;
                  u.Birdday = BasicDatePicker1.SelectedDate;
                  u.Gender = Convert.ToBoolean(gender);
                  u.Address = txtAddress.Text;
                  u.Phone = txtPhone.Text;
                  u.Email = txtEmail.Text;
                  u.Position = ddlPosition.SelectedValue;
                  u.ImageUrl = "images/" + Session["img"].ToString();
                  u.Description = txtDescription.Text;
                  u.Type = Convert.ToInt32(ddlTitle.SelectedValue);
                  u.Status = Convert.ToBoolean(status);
                  db.Users.InsertOnSubmit(u);
                  db.SubmitChanges();
                  Session["img"] = "";
                  lblSuccess.Text = "Chuc mung ban da dang ky thanh cong...";
                  Panel1.Visible = true;
                  Panel2.Visible = false;
              }
        }
        catch {
            Panel1.Visible = false;
            lblFalse.Text = "Dang ky that bai,moi ban dang ky lai...";
            Panel2.Visible = true;
        }
       
    }
    protected void btnSua_Click(object sender, EventArgs e)
    {
        
        /*SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["hoangthinh"].ConnectionString;
        SqlTransaction transaction;
        if (rdoNam.Checked)
        {
                gender = 1;
        }
        if (rdoLock.Checked) {
                status = 0;
        }
        var u = con.User.Where(c=>c.UserID == txtUsername.Text).SingleOrDefault();
        if (u != null)
        {
                 
               
                 u.Password = txtPassword.Text;
                 u.FullName = txtFullName.Text;
                 u.Birdday = BasicDatePicker1.SelectedDate;
                 u.Gender = Convert.ToBoolean(gender);
                 u.Address = txtAddress.Text;
                 u.Phone = txtPhone.Text;
                 u.Email = txtEmail.Text;
                 u.Position = ddlPosition.SelectedValue;
                 u.ImageUrl = "images/" + Session["img"].ToString();
                 u.Description = txtDescription.Text;
                 u.Type = Convert.ToInt32(ddlTitle.SelectedValue);
                 u.Status = Convert.ToBoolean(status);
                 con.Users.InsertOnSubmit(u);
                 con.SubmitChanges();
                 Session["img"] = "";
                 lblSuccess.Text = "Chuc mung ban da sau thanh cong...";
                 Panel1.Visible = true;
                 Panel2.Visible = false;
        }*/
    }
}